﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HKEA.CMS.Core.IDAL.App;
using HKEA.CMS.Core.Models;
using HKEA.CMS.Core.DALFactory;
namespace HKEA.CMS.Core.DAL.App
{
    public class app_tblCustomerDAL:Iapp_tblCustomer
    {
        HKEA_CMSEntities model;
        app_tblCustomer customer;
        private app_tblCustomerDAL()
        {
            model = DataAccess.GetModelInstance();
        }

        public static app_tblCustomerDAL GetInstance()
        {
            return new app_tblCustomerDAL();
        }

        public int InsertENT(app_tblCustomer ent)
        {
            ent.CreationDate = DateTime.Now;
            ent.LastModifyDate = DateTime.Now;
            model.app_tblCustomer.AddObject(ent);
            if (model.SaveChanges() > 0)
                return model.app_tblCustomer.Max(a=>a.AppCustomerUID);
            return 0;
        }

        public app_tblCustomer GetEntity(int UID) {
            return model.app_tblCustomer.Where(a => a.CustomerUID == UID).SingleOrDefault();
        }

        public List<app_tblCustomer> GetListByENT(app_tblCustomer ent)
        {
            var result = model.app_tblCustomer.Where(a=>a.AppCustomerUID > 0);
            if (ent.AppCustomerUID > 0)
                result = result.Where(a=>a.AppCustomerUID==ent.AppCustomerUID);
            if (ent.CustomerUID > 0)
                result = result.Where(a=>a.CustomerUID==ent.CustomerUID);
            if (!string.IsNullOrEmpty(ent.CustomerID))
                result = result.Where(a=>a.CustomerID==ent.CustomerID);
            return result.ToList();
        }

        public int GetCountByENT(app_tblCustomer ent)
        {
            var result = model.tblCustomers.Where(a => a.CustomerUID > 0);
            if (!string.IsNullOrEmpty(ent.CustomerID))
                result = result.Where(a => a.CustomerID == ent.CustomerID);
            if (!string.IsNullOrEmpty(ent.Name_Eng))
                result = result.Where(a => a.Name_Eng.Contains(ent.Name_Eng));
            return result.Count();
        }

        public int UpdateDeleted(int UID)
        {
            var result = model.tblCustomers.Where(a => a.CustomerUID == UID).SingleOrDefault();
            if (result != null)
            {
                result.Deleted = 1;
            }
            return model.SaveChanges();
        }

        public int UpdateENT(app_tblCustomer ent)
        {
            var result = model.app_tblCustomer.Where(a => a.AppCustomerUID == ent.AppCustomerUID).FirstOrDefault();
            if (result != null)
            {
                result.AddressUID = ent.AddressUID;
                result.AppAddressUID = ent.AppAddressUID;
                result.AppContactInfoUID = ent.AppContactInfoUID;
                result.BR = ent.BR;
                result.ContactInfoUID = ent.ContactInfoUID;
                result.CustomerID = ent.CustomerID;
                result.CustomerSince = ent.CustomerSince;
                result.CustomerUID = ent.CustomerUID;
                result.CustTypeUID = ent.CustTypeUID;
                result.EstDate = ent.EstDate;
                result.HKID_PassportNo = ent.HKID_PassportNo;
                result.HQLocatedinHK = ent.HQLocatedinHK;
                result.Intro_Chi = ent.Intro_Chi;
                result.Intro_Eng = ent.Intro_Eng;
                result.IsGCMember = ent.IsGCMember;
                result.IsMember = ent.IsMember;
                result.IsVIP = ent.IsVIP;
                result.LastModifyDate = DateTime.Now;
                result.Name_Chi = ent.Name_Chi;
                result.Name_Eng = ent.Name_Eng;
                result.NoOfEmp = ent.NoOfEmp;
                result.ReferralBy = ent.ReferralBy;
                result.ReferralDate = ent.ReferralDate;
                result.Stroke = ent.Stroke;
                result.Terminated = ent.Terminated;
            }
            return model.SaveChanges();
        }
         
        public int DeleteENT(app_tblCustomer ent)
        {
            var temp = model.tblCustomers.Where(a => a.CustomerUID > 0);
            if (ent.CustomerUID > 0)
                temp = temp.Where(a => a.CustomerUID == ent.CustomerUID);
            if (!string.IsNullOrEmpty(ent.CustomerID))
                temp = temp.Where(a => a.CustomerID == ent.CustomerID);
            var list = temp.ToList();
            for (int i = 0; i < list.Count(); i++)
            {
                model.tblCustomers.DeleteObject(list[i]);
            }
            return model.SaveChanges();
        }

        public int DeleteENT(int UID)
        {
            var result = model.tblCustomers.Where(a => a.CustomerUID == UID).SingleOrDefault();
            if (result != null)
            {
                model.tblCustomers.DeleteObject(result);
            }
            return model.SaveChanges();
        }
        public List<int> GetCustomerUIDList()
        {
            return model.app_tblCustomer.Where(a => a.CustomerUID > 0).Select(a => a.CustomerUID).ToList();
        }
    }
}
